package com.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class PlusTest {
	public static void main(String[] args) throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

		//2.获取sqlSession实例，能直接执行已经映射的sql语句
		SqlSession sqlSession = sqlSessionFactory.openSession();

		EmployeeMapperPlus mapper = sqlSession.getMapper(EmployeeMapperPlus.class);
		DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);

		singleResultMapTest(mapper);
		//MultiResultMapTest(mapper);
//		stepQueryById(mapper);
		//collectionQueryById(departmentMapper);
//		collectionQueryByIdStep(departmentMapper);

		sqlSession.close();
	}

	private static void collectionQueryByIdStep(DepartmentMapper departmentMapper) {
		Department deptByIdStep = departmentMapper.getDeptByIdStep(1);
		List<Employee> emps = deptByIdStep.getEmps();
		System.out.println(emps);
	}

	private static void collectionQueryById(DepartmentMapper mapper) {
		Department deptByIdPlus = mapper.getDeptByIdPlus(1);
		System.out.println(deptByIdPlus);
		List<Employee> emps = deptByIdPlus.getEmps();
		System.out.println(emps);
	}

	private static void stepQueryById(EmployeeMapperPlus mapper) {
		Employee empByIdStep = mapper.getEmpByIdStep(7);
		System.out.println(empByIdStep);
		Employee empByIdStep2 = mapper.getEmpByIdStep(9);
		System.out.println(empByIdStep2);
	}

	private static void MultiResultMapTest(EmployeeMapperPlus mapper) {
		Employee empById = mapper.getEmpAndDept(7);
		System.out.println(empById);
	}

	private static void singleResultMapTest(EmployeeMapperPlus mapper) {
		Employee empById = mapper.getEmpById(7);
		System.out.println(empById);
	}
}
